package com.jinqing.wuchuang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.data.style.Style;
import com.jinqing.wuchuang.entity.FdfsFile;
import com.jinqing.wuchuang.entity.SettleCompany;
import com.jinqing.wuchuang.entity.SettledLegalRepresentative;
import com.jinqing.wuchuang.entity.SettledTeamMenber;
import com.jinqing.wuchuang.mapper.FdfsFileMapper;
import com.jinqing.wuchuang.mapper.SettleCompanyMapper;
import com.jinqing.wuchuang.mapper.SettledLegalRepresentativeMapper;
import com.jinqing.wuchuang.mapper.SettledTeamMenberMapper;
import com.jinqing.wuchuang.service.FileService;
import com.jinqing.wuchuang.service.WordService;
import com.jinqing.wuchuang.utils.WordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WordServiceImpl implements WordService {

    @Autowired
    SettledTeamMenberMapper settledTeamMenberMapper;
    @Autowired
    SettleCompanyMapper settleCompanyMapper;
    @Autowired
    SettledLegalRepresentativeMapper settledLegalRepresentativeMapper;
    @Autowired
    FileService fileService;

    private static String[] industries = {"现代农业","制造业","信息技术服务","文化创意服务","社会服务","公益创业"};
    private static String[] dreamWorks = {"X-SPACE创业孵化服务梦工场","陶瓷艺术梦工场","新能源与智能汽车梦工场","大型邮轮游艇梦工场","新材料梦工场","绿色建材梦工场","大数据与人工智能梦工场","现代金融与电子商务梦工场"};

    /**
     * 返回文件流的形式
     * @param response
     * @param uid       老板的uid
     * @param companyId 公司的id
     * @return
     * @throws IOException
     */
    @Override
    public String creatWordById(HttpServletResponse response,Long uid, int companyId) throws IOException {
        Map data = this.getData(uid,companyId);
        Map params = this.creatWordParam((SettledLegalRepresentative)data.get("rep"),(SettleCompany) data.get("c"),(List<SettledTeamMenber>)data.get("m"));
        String creatResult = WordUtil.createWord(response,params);
        return creatResult;
    }

    /**
     * 返回文件网址的形式
     * @param uid
     * @param companyId
     * @return
     * @throws IOException
     */
    @Override
    public String creatWordURL(Long uid, int companyId) throws IOException {
        Map data = this.getData(uid,companyId);
        Map params = this.creatWordParam((SettledLegalRepresentative)data.get("rep"),(SettleCompany) data.get("c"),(List<SettledTeamMenber>)data.get("m"));
        MultipartFile multipartFile = WordUtil.creatWord(params);
        String url = fileService.wordURL(multipartFile,uid,companyId);
        return url;
    }

    /**
     * 从数据库获取信息
     * @param uid
     * @param companyId
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Map getData(Long uid, int companyId) {
        // 查老板
        QueryWrapper<SettledLegalRepresentative> legalQueryWrapper = new QueryWrapper<>();
        legalQueryWrapper.eq("uid",uid);
        SettledLegalRepresentative rep = settledLegalRepresentativeMapper.selectOne(legalQueryWrapper);
        // 查公司
        QueryWrapper<SettleCompany> companyQueryWrapper = new QueryWrapper<>();
        companyQueryWrapper.eq("uid",uid).eq("id",companyId);
        SettleCompany company = settleCompanyMapper.selectOne(companyQueryWrapper);
        // 查成员
        QueryWrapper<SettledTeamMenber> menbersQueryWrapper = new QueryWrapper<>();
        menbersQueryWrapper.eq("leader_id",uid).eq("company_id",companyId);
        List<SettledTeamMenber> menbers = settledTeamMenberMapper.selectList(menbersQueryWrapper);
        Map map = new HashMap();
        map.put("rep",rep);
        map.put("c",company);
        map.put("m",menbers);
        return map;
    }


    /**
     *  创建模板数据，用来填充word模板
     * @param rep
     * @param company
     * @param menbers
     * @return
     */
    @Override
    public Map creatWordParam(SettledLegalRepresentative rep, SettleCompany company, List<SettledTeamMenber> menbers) {
        // 成员人数小于5人时，给模板员工凑够五个位置
        if(menbers.size() < 5){
            for (int i = 0; i < 5-menbers.size(); i++) {
                menbers.add(new SettledTeamMenber());
            }
        }
        // map的数据最终放到模板中
        Map params = new HashMap();
        params.put("rep",rep);
        params.put("c",company);
        params.put("m",menbers);
        // 给框框打√
        params.put("isGraduate0",new TextRenderData("在校生".equals(rep.getIsGraduate())?"\u2611":"\u2610",new Style("宋体",12)));
        params.put("isGraduate1",new TextRenderData("在校生".equals(rep.getIsGraduate())?"\u2610":"\u2611",new Style("宋体",12)));
        params.put("isRegister0",new TextRenderData("已注册".equals(company.getIsRegister())?"\u2611":"\u2610",new Style("宋体",12)));
        params.put("isRegister1",new TextRenderData("已注册".equals(company.getIsRegister())?"\u2610":"\u2611",new Style("宋体",12)));
        // 给行业前面的框框打√
        int industryCount = 0;
        for (int i = 0; i < industries.length; i++) {
            if(industries[i].equals(company.getIndustry())){
                params.put("industry"+i,new TextRenderData("\u2611",new Style("宋体",12)));//打√
            }else{
                params.put("industry"+i,new TextRenderData("\u2610",new Style("宋体",12)));
                industryCount ++ ;

            }
        }
        // 如果填的是其他行业
        System.out.println("industryCount" + industryCount + "in"+industries.length);
        if(industryCount == industries.length){
            params.put("industry"+(industries.length),new TextRenderData("\u2611",new Style("宋体",12)));
            params.put("industry"+(industries.length+1),company.getIndustry());
        }else{
            params.put("industry"+(industries.length),new TextRenderData("\u2610",new Style("宋体",12)));
            params.put("industry"+(industries.length+1),"");
        }
        // 给梦工厂前面的框框打√
        int dreamWorkCount = 0;
        for (int i = 0; i < dreamWorks.length; i++) {
            if(dreamWorks[i].equals(company.getDreamWork())){
                params.put("dream"+i,new TextRenderData("\u2611",new Style("宋体",12)));//打√
            }else{
                params.put("dream"+i,new TextRenderData("\u2610",new Style("宋体",12)));
                dreamWorkCount ++ ;
            }
        }
        // 如果填的是其他梦工厂
        if(dreamWorkCount == dreamWorks.length){
            params.put("dream"+dreamWorks.length,new TextRenderData("\u2611",new Style("宋体",12)));
            params.put("dream"+(dreamWorks.length+1),company.getDreamWork());
        }else {
            params.put("dream"+(dreamWorks.length),new TextRenderData("\u2610",new Style("宋体",12)));
            params.put("dream"+(dreamWorks.length+1),"");
        }
        return params;
    }
}
